package com.tza.web.t_net.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.FloatItem;
import com.smartgwt.client.widgets.form.fields.IntegerItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.VLayout;
import com.tza.web.t_net.client.sceens.SimpleGrid;
import com.tza.web.t_net.client.screenservices.LocationHistoryService;
import com.tza.web.t_net.client.screenservices.LocationHistoryServiceAsync;
import com.tza.web.t_net.shared.entity.DataMap;

public class MainScr extends Composite
{

   ActionContext                             actionContext;

   private final LocationHistoryServiceAsync sampleService = (LocationHistoryServiceAsync) GWT.create(LocationHistoryService.class);

   private VLayout                           layout        = new VLayout(30);
   private VLayout                           layout1       = new VLayout(30);
   private SimpleGrid                        newGridToTest;
   private DynamicForm                       form          = new DynamicForm();
   private AsyncCallback                     callback;

   public MainScr(ActionContext mainActionContext)
   {
      // Initialisation du widget
      initWidget(layout);

      final ListGrid activityGrid = new ListGrid();
      // activityGrid.setWidth(800);
      // activityGrid.setHeight(800);
      activityGrid.setShowAllRecords(true);
      queryData(activityGrid);
      ListGridField countryCodeField = new ListGridField("countryCode", "Code", 40);
      ListGridField nameField = new ListGridField("countryName", "Country");
      ListGridField independenceField = new ListGridField("independence", "Nationhood", 225);
      independenceField.setType(ListGridFieldType.DATE);
      ListGridField populationField = new ListGridField("population", "Population");
      populationField.setType(ListGridFieldType.INTEGER);
      ListGridField gdpField = new ListGridField("gdp", "GDP ($B)");
      gdpField.setType(ListGridFieldType.FLOAT);

      activityGrid.setFields(new ListGridField[]
      { countryCodeField, nameField, independenceField, populationField, gdpField });
      activityGrid.setCanResizeFields(true);

      int w = 800;
      ListGridField[] gf = new ListGridField[]
      { countryCodeField, nameField, independenceField, populationField, gdpField };
      int h = 800;
      newGridToTest = new SimpleGrid(mainActionContext, w, h, gf);
      ButtonItem query = new ButtonItem();
      query.setTitle("query");

      query.addClickHandler(new ClickHandler()
      {
         public void onClick(ClickEvent event)
         {
            newGridToTest.LoadData("Test", null);
         }
      });
      newGridToTest.LoadData("Test", null);
      // Ajout du formulaire
      layout.addMember(newGridToTest);

      ButtonItem addItem = new ButtonItem();
      addItem.setTitle("Add");

      final TextItem countryCodeFieldF = new TextItem("Code");
      final TextItem nameFieldF = new TextItem("Country");
      final DateItem independenceFieldF = new DateItem("Nationhood");

      final IntegerItem populationFieldF = new IntegerItem("Population");
      final FloatItem gdpFieldF = new FloatItem("GDP");

      form.setFields(countryCodeFieldF, nameFieldF, independenceFieldF, populationFieldF, gdpFieldF, query);

      addItem.addClickHandler(new ClickHandler()
      {
         public void onClick(ClickEvent event)
         {
            ListGridRecord record = new ListGridRecord();
            record.setAttribute("countryCode", countryCodeFieldF.getValueAsString());
            record.setAttribute("countryName", nameFieldF.getValueAsString());
            record.setAttribute("independence", independenceFieldF.getValueAsDate());

            record.setAttribute("population", Integer.parseInt((String) populationFieldF.getValue()));
            record.setAttribute("gdp", Float.parseFloat((String) gdpFieldF.getValue()));
            activityGrid.addData(record);
         }
      });

      // this.setVisible(false);
      actionContext = mainActionContext;
      actionContext.getMainEventBus().addHandler(AuthenticationEvent.TYPE, new AuthenticationEventHandler()
      {
         public void onAuthenticationChanged(AuthenticationEvent authenticationEvent)
         {
            changeToVisible();
         }
      });

      layout.addMember(form);
      // Affichage du Widget
      layout.redraw();

   }

   private void queryData(ListGrid activityGrid)
   {

      final ListGridRecord record = new ListGridRecord();

      sampleService.getLocationHistory("dummy", new AsyncCallback<List<DataMap>>()
      {

         public void onFailure(Throwable caught)
         {
            Window.alert("RPC to sendEmail() failed.");
         }

         public void onSuccess(List<DataMap> result)
         {
            for (DataMap dataMap : result)
            {
               record.setAttribute("code", dataMap.get("code"));
               record.setAttribute("countryName", dataMap.get("countryName"));
               record.setAttribute("independence", dataMap.get("independence"));

               record.setAttribute("population", dataMap.get("population"));
               record.setAttribute("gdp", dataMap.get("gdp"));

            }

         }
      });

      activityGrid.addData(record);
   }

   public void changeToVisible()
   {
      this.setVisible(true);
   }

}
